<?php

/**
 * @property string $id
 * @property string $mp_appid
 * @property string $mp_key
 * @property string $mp_mchid
 * @property string $totalfee
 * @property string $order_number
 * @property string $prepay_id
 * @property string $openid
 * @property string $notify_url
 * @property string $return_url
 * @property string $payment_desc
 * @property integer $payment_status
 * @property string $payment_time
 * @property string $post_payment_time
 * @property integer $payment_errcode
 * @property string $payment_errmsg
 * @property string $create_time
 * @property string $update_time
 * @author lovebing <lovebing365@126.com> Created on 2015-4-15
 */
class PaymentWechatModel extends MainActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 't_payment_wechat';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('mp_appid, mp_key, mp_mchid', 'required'),
            array('payment_status, payment_errcode', 'numerical', 'integerOnly' => true),
            array('mp_appid, mp_mchid', 'length', 'max' => 50),
            array('mp_key, order_number, prepay_id, openid, payment_errmsg', 'length', 'max' => 100),
            array('totalfee', 'length', 'max' => 10),
            array('payment_desc', 'length', 'max' => 500),
            array('notify_url, return_url', 'length', 'max' => 1000),
            array('payment_time, post_payment_time, create_time', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, mp_appid, mp_key, mp_mchid, totalfee, order_number, prepay_id, openid, notify_url, return_url, payment_desc, payment_status, payment_time, post_payment_time, payment_errcode, payment_errmsg, create_time, update_time', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'mp_appid' => 'Mp Appid',
            'mp_key' => 'Mp Key',
            'mp_mchid' => 'Mp Mchid',
            'totalfee' => 'Totalfee',
            'order_number' => 'Order Number',
            'prepay_id' => 'Prepay',
            'openid' => 'Openid',
            'notify_url' => 'Notify Url',
            'return_url' => 'Return Url',
            'payment_desc' => 'Payment Desc',
            'payment_status' => 'Payment Status',
            'payment_time' => 'Payment Time',
            'post_payment_time' => 'Post Payment Time',
            'payment_errcode' => 'Payment Errcode',
            'payment_errmsg' => 'Payment Errmsg',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return PaymentWechatModel the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 
     * @param type $id
     * @return PaymentWechatModel
     */
    public static function findById($id) {
        return self::model()->findByPk($id);
    }


    public static function findByOrderNumber($number){
    	$c = new CDbCriteria();
    	$c->addCondition("order_number = '$number' ");
    	$c->order = "id desc";  
    	$model = self::model()->find($c);
    	return $model ;
    }
}
